﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;

public partial class LoginStudent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("LogoutButton").Visible = false;
    }

    public bool AuthenticateActiveDirectory(string Domain, string UserName, string Password)
    {
        try
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain, UserName, Password);
            object nativeObject = entry.NativeObject;
            return true;
        }
        catch (Exception ex) { return false; }
    }

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {

        bool auth = AuthenticateActiveDirectory("www.iiitd.edu.in:389", Login1.UserName, Login1.Password);
        if (auth == true)
        {
            e.Authenticated = true;
            Session.Add("Username", Login1.UserName);
            Response.Redirect("~/Student/WelcomeStudentUI.aspx");
        }

        else
        {
            e.Authenticated = false;
            Session.Add("Username", null);
            //Response.Redirect("~/Admin/WelcomeAdminUI.aspx");
        }

    }
}